import 'package:flutter/material.dart';
import 'package:flutter_account_book/utils/db/entities/account_record.dart';
import 'package:flutter_account_book/utils/db/entities/pay_method.dart';
import 'package:flutter_account_book/utils/db/entities/record_type.dart';
import 'package:flutter_account_book/widgets/custom_icon.dart';
import 'package:get/get.dart';
import 'package:flutter_account_book/utils/db/db_helper.dart';
import 'package:flutter_account_book/utils/db/entities/account_book.dart';

import 'state.dart';

class MyHomeLogic extends GetxController {
  final state = MyHomeState();

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    _loadData();
  }

  _loadData() async {
    await _getAccountBookList();
    state.curId.value = AccountBook.idAll;
    AccountBook? book =
        await DbHelper.helper.accountBookDao.getById(state.curId.value);
    state.title.value = book?.name ?? "";
  }

  _getAccountBookList() async {
    state.accountBooks.value = await DbHelper.helper.accountBookDao.flowAll();
  }

  reload() async {
    await _getAccountBookList();
  }

  Future<List<AccountRecord>> getAccountRecordList(int id) async {
    if (id == AccountBook.idAll) {
      return DbHelper.helper.accountRecordDao.flowAll();
    }
    return DbHelper.helper.accountRecordDao.flowByBookId(id);
  }

  Future<double> getTotalIncome(int id) async {
    if (id == AccountBook.idAll) {
      return DbHelper.helper.accountRecordDao.getTotalIncome();
    }
    return DbHelper.helper.accountRecordDao.getTotalIncomeByBookId(id);
  }

  Future<double> getTotalExpend(int id) async {
    if (id == AccountBook.idAll) {
      return DbHelper.helper.accountRecordDao.getTotalExpend();
    }
    return DbHelper.helper.accountRecordDao.getTotalExpendByBookId(id);
  }

  Future<double> getCurMonthTotalIncome(int id) async {
    if (id == AccountBook.idAll) {
      return DbHelper.helper.accountRecordDao.getTotalCurMonthIncome();
    }
    return DbHelper.helper.accountRecordDao.getTotalCurMonthIncomeByBookId(id);
  }

  Future<double> getCurMonthTotalExpend(int id) async {
    if (id == AccountBook.idAll) {
      return DbHelper.helper.accountRecordDao.getTotalCurMonthExpend();
    }
    return DbHelper.helper.accountRecordDao.getTotalCurMonthExpendByBookId(id);
  }

  _loadRecordType(int recordTypeId) {
    DbHelper.helper.recordTypeDao.getById(recordTypeId).then((record) {
      if (record != null) {
        state.recordTypes.addIf(true, "${record.id}", record);
      }
    });
  }

  _loadPayMethod(int payMethodId) {
    DbHelper.helper.payMethodDao.getById(payMethodId).then((record) {
      if (record != null) {
        state.payMethods.addIf(true, "${record.id}", record);
      }
    });
  }

  buildIcon(int recordTypeId, ThemeData themeData) {
    return Obx(() {
      String key = "$recordTypeId";
      RecordType? recordType = state.recordTypes[key];
      if (recordType == null) {
        _loadRecordType(recordTypeId);
      }
      return recordType == null
          ? Container(width: 40, height: 40, color: Colors.grey)
          : recordType.type == 0
              ? CustomIcon.asset(recordType.iconPath,
                  size: CustomIconSize.middle,
                  backgroundColor: Color(recordType.backgroundColorHex),
                  color: Color(recordType.textColorHex))
              : CustomIcon.text(recordType.iconPath,
                  size: CustomIconSize.middle,
                  backgroundColor: Color(recordType.backgroundColorHex),
                  color: Color(recordType.textColorHex));
    });
  }

  buildPayIcon(int payMethodId) {
    return Obx(() {
      String key = "$payMethodId";
      PayMethod? recordType = state.payMethods[key];
      if (recordType == null) {
        _loadPayMethod(payMethodId);
      }
      return recordType == null
          ? Container(width: 20, height: 20, color: Colors.grey)
          : Image.asset(recordType.iconPath ?? "", width: 20, height: 20);
    });
  }
}
